package com.lagou.utils;


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BeanFactory {
    private static Map<String,Object> iocmap = new HashMap<String,Object>();
    // 程序启动时,初始化对象实例
    static{
        // 1.读取配置文件
        InputStream resourceAsStream = BeanFactory.class.getClassLoader().getResourceAsStream("beans.xml");
        // 2.解析xml(dom4j)
        SAXReader saxReader = new SAXReader();
        try {
            Document document = null;
            try {
                document = saxReader.read(resourceAsStream);

                // 3.编写Xpath表达式 用来读取里边的bean对象
                String path = "//bean";
                // 4.获取到所有的bean标签
                List<Element> list = document.selectNodes(path);
                // 遍历并使用反射创建对象实例,存到map集合(ioc)容器中
                for (Element element : list) {
                    String id = element.attributeValue("id");
                    // className com.lagou.dao.impl.UserDaoImpl
                    String aClass = element.attributeValue("class");
                    // 使用反射生成实例对象
                    Object o = Class.forName(aClass).newInstance();
                    iocmap.put(id,o);
                }
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // 获取bean的方法
    public static Object getBean(String beanId){
        Object o = iocmap.get(beanId);
        return o;
    }
}
